﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Device.Location;
using Microsoft.Phone.Controls;

namespace testTopBar
{
    public partial class MainPage : PhoneApplicationPage
    {
        public MainPage()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            createTopBar("天氣資訊");
        }

        public void createTopBar(string titleTetxt)
        {
            // create topbar(stackPanel, it is a container)
            StackPanel topBar = new StackPanel();
            // set UI properties
            topBar.Width = 480;
            topBar.Height = 88;
            // set orientation
            topBar.Orientation = System.Windows.Controls.Orientation.Horizontal;

            // create background ImageSource
            ImageBrush tmpImgBrush = new ImageBrush();            
            tmpImgBrush.ImageSource = new BitmapImage(new Uri(@"topbar.png", UriKind.Relative));
            topBar.Background = tmpImgBrush;
            
            // create back button
            Button tmpBtn = new Button();
            // create background ImageSource
            ImageBrush btnBrush = new ImageBrush();
            btnBrush.ImageSource = new BitmapImage(new Uri(@"bt_back.png", UriKind.Relative));
            tmpBtn.Background = btnBrush;

            // set UI properties
            tmpBtn.Width = 88;
            tmpBtn.Height = 88;
            tmpBtn.BorderBrush = new SolidColorBrush(Colors.Transparent);
            tmpBtn.VerticalAlignment = System.Windows.VerticalAlignment.Center;
            tmpBtn.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;

            // add to stackpanel(topbar)
            topBar.Children.Add(tmpBtn);

            // create a textblock
            TextBlock tmpTextBlock = new TextBlock();
            tmpTextBlock.Text = titleTetxt;

            // set UI properties
            tmpTextBlock.Width = 250;
            tmpTextBlock.Height = 70;
            tmpTextBlock.VerticalAlignment = System.Windows.VerticalAlignment.Center;
            tmpTextBlock.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
            tmpTextBlock.TextAlignment = TextAlignment.Left;
            tmpTextBlock.FontSize = 48;            
            // add to stackPanel(topBar)
            topBar.Children.Add(tmpTextBlock);

            // create a button
            tmpBtn = new Button();

            // create background ImageSource
            btnBrush = new ImageBrush();
            btnBrush.ImageSource = new BitmapImage(new Uri(@"bt_update.png", UriKind.Relative));
            tmpBtn.Background = btnBrush;

            // set UI properties
            tmpBtn.Width = 141;
            tmpBtn.Height = 70;
            tmpBtn.BorderBrush = new SolidColorBrush(Colors.Transparent);
            tmpBtn.VerticalAlignment = System.Windows.VerticalAlignment.Center;
            tmpBtn.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;

            // add to stackPanel(topBar)
            topBar.Children.Add(tmpBtn);

            // add topBar to background Canvas
            backCanvas.Children.Add(topBar);

            // set position of topBar(relative to Canvas)
            Canvas.SetLeft(topBar, 0);
            Canvas.SetTop(topBar, 0);        
        }
    }
}
